package com.briup.srms.cms.web.controller;

import com.briup.srms.cms.bean.Article;
import com.briup.srms.cms.bean.extend.ArticleExtend;
import com.briup.srms.cms.service.IArticleService;
import com.jacky.base.utils.ExcelUtils;
import com.jacky.base.utils.Message;
import com.jacky.base.utils.MessageUtil;
import com.jacky.base.utils.PageVM;
import com.briup.srms.cms.vm.CateoryAriticleVM;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: 杰普基础管理系统
 * @description: 消息控制器类
 * @author: charles
 * @create: 2020-11-12 10:22
 **/
@Api(description="消息相关接口")
@Validated
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private IArticleService articleService;

    @ApiOperation(value = "查询栏目文章信息")
    @GetMapping("queryCateoryAriticleVM")
    public Message queryCateoryAriticleVM(){
       List<CateoryAriticleVM> list = articleService.queryCateoryAriticleVM();
       return MessageUtil.success(list);
    }

    @ApiOperation(value = "分页多条件获取学生风采信息")
    @GetMapping("pageQuery")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, paramType = "query"),
        @ApiImplicitParam(name = "title", value = "学生风采标题", paramType = "query"),
        @ApiImplicitParam(name = "status", value = "风采状态", paramType = "query"),
        @ApiImplicitParam(name = "authorId", value = "作者id", paramType = "query"),
        @ApiImplicitParam(name = "categoryId", value = "分类id", paramType = "query")
	})
    public Message pageQuery(int page, int pageSize, String title, String status,Long authorId, Long categoryId){
       PageVM<ArticleExtend> pageVM = articleService.pageQuery(page, pageSize, title, status,authorId, categoryId);
       return MessageUtil.success(pageVM);
    }

    @ApiOperation(value="将消息导入到Excel中",notes="注意！测试的时候请将地址粘贴到浏览器地址栏测试",produces="application/octet-stream")
    @GetMapping("download")
    public void download(HttpServletResponse response) throws Exception{
        // 查询出所有消息信息
        String excelName = "article_list";
        String[] headList = new String[]{"编号","标题","内容"};
        String[] fieldList = new String[]{"id","title","content"};
        List<Map<String, Object>> dataList = new ArrayList<>();
        PageVM<ArticleExtend> pageVM =articleService.pageQuery(1, 100,null,null,null,null);
        for(Article a : pageVM.getList()){
            Map<String, Object> map = new HashMap<>();
            map.put("id",a.getId());
            map.put("title",a.getTitle());
            map.put("content",a.getContent());
            dataList.add(map);
        }
        //调用工具类导出excel
        ExcelUtils.createExcel(response,excelName,headList,fieldList,dataList);
    }

    @ApiOperation(value = "通过id查询")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id",value = "主键",paramType = "query")
    )
    @GetMapping("findById")
    public Message findById(long id){
        ArticleExtend articleExtend = articleService.findById(id);
        return MessageUtil.success(articleExtend);
    }

    @ApiOperation(value = "通过id删除")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id",value = "主键",paramType = "query",required=true)
    )
    @GetMapping("deleteById")
    public Message deleteById(long id){
    	articleService.deleteById(id);
        return MessageUtil.success("删除成功");
    }

    @ApiOperation(value = "保存或更新消息信息",notes = "如果参数中包含id后端认为是更新操作，如果参数中不包含id认为是插入操作")
    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(Article article){
        articleService.saveOrUpdate(article);
        return MessageUtil.success("更新成功");
    }

}
